package com.taxicall.taxi;

import java.net.SocketException;

import udptransport.UdpNetworkService;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class NetworkService extends Service {

	class LocalBinder extends Binder {
		NetworkService getService(){
			return NetworkService.this;
		}
	}

	private final IBinder binder = new LocalBinder();
	private UdpNetworkService network;

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	// Called when all the clients disconnect
	@Override
	public boolean onUnbind(Intent intent) {
		close();
		return super.onUnbind(intent);
	}

	public UdpNetworkService open() throws SocketException {
		// initialize and return the udp network service if it is null
		if (network == null) {
			network = new UdpNetworkService();
		}
		return network;
	}

	public void close() {
		if (network != null)
			network.close();
	}

}
